package timer

import (
	"log"
	"time"
)

//GetNowTime 获取当前时间
func GetNowTime() time.Time {
	location, _ := time.LoadLocation("Asia/Shanghai")
	return time.Now().In(location)
}

//GetCalculateTime 计算时间
func GetCalculateTime(currentTimer time.Time, d string) (time.Time, error) {
	// 从字符串解析出持续时间,列如: "300ms", "-1.5h", "2h45m"
	duration, err := time.ParseDuration(d)
	if err != nil {
		return time.Time{}, err
	}

	return currentTimer.Add(duration), nil
}

func example() {
	location, _ := time.LoadLocation("Asia/Shanghai")
	inputTime := "2029-09-04 12:02:33"
	layout := "2006-01-02 15:04:05"

	t, _ := time.Parse(layout, inputTime)
	dateTime := time.Unix(t.Unix(), 0).In(location).Format(layout)
	log.Printf("输入时间:%s, 输出时间:%s", inputTime, dateTime)
	// 2020/09/08 23:11:55 输入时间:2029-09-04 12:02:33, 输出时间:2029-09-04 20:02:33

	// 解决方法

	t, _ = time.ParseInLocation(layout, inputTime, location)
	dateTime = time.Unix(t.Unix(), 0).In(location).Format(layout)
	// 2020/09/08 23:14:31 输入时间:2029-09-04 12:02:33, 输出时间:2029-09-04 12:02:3
	log.Printf("输入时间:%s, 输出时间:%s", inputTime, dateTime)

}
